package com.jd.org.controller;


import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Employee;
import com.jd.org.query.EmployeeQuery;
import com.jd.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的接口类",description = "员工的接口类")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    // 查询id
    @ApiOperation(value = "查询一个")
    @GetMapping("{id}")
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    // 查询全部
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }


    // 删除
    @ApiOperation(value = "查询删除")
    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    // 添加和修改
    @ApiOperation(value = "查询添加和修改")
    @PutMapping
    public AjaxResult addUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    // 分页查询
    @ApiOperation(value = "查询分页")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }


    // 批量删除
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

//    // 查询部门树
//    @ApiOperation(value = "查询部门树")
//    @GetMapping("/deptTree")
//    public List<Employee> deptTree(){
//        return employeeService.deptTree();
//    }


    @ApiOperation(value = "根据店铺id查询所以店铺工作人员")
    @GetMapping("/list/{id}")
    public List<Employee> findShopId(@PathVariable("id")Long id){
        return employeeService.findShopId(id);
    }




}
